# Import 
import wx

# ------------------------
class RubberBand(wx.Panel):
# ------------------------
	# ------------------------------------------------
	def __init__(self, image, parent, callback = None):
	# ------------------------------------------------
		wx.Panel.__init__(self, id = wx.ID_ANY, parent = parent, size =(640, 480), style = wx.EXPAND)

		# Store the image and the callback
		self.image = image
		self.callback = callback

		# Setup the image for drawing
		self.firstPt = (0,0)
		self.drawing = False
		self.backgroundRects = []
		self.foregroundRects = []

		# Listen to mouse/paint events
		self.Bind(wx.EVT_LEFT_DOWN, self.onLeftMouseDown)
		self.Bind(wx.EVT_LEFT_UP, self.onLeftMouseUp)
		self.Bind(wx.EVT_MOTION, self.onMouseMove)
		self.Bind(wx.EVT_PAINT, self.onPaint)	

		# Create our colours
		self.backgroundColor = wx.Colour(0, 100, 0)
		self.foregroundColor = wx.Colour(0, 255, 0)
		self.penWidthPx = 3

		# Finally, enable the control
		self.Enable()
		self.Layout()
	
	# ------------------------
	def setImage(self, image):
	# ------------------------
		self.image = image
		self.backgroundRects = []
		self.foregroundRects = []
		self.Refresh()
		
	# -----------------------------
	def setCallback(self, callback):
	# -----------------------------
		self.callback = callback

	# -------------------------------------------
	def setBackgroundRects(self, backgroundRects):
	# -------------------------------------------
		self.backgroundRects = backgroundRects

	# -------------------------------------------
	def setForegroundRects(self, foregroundRects):
	# -------------------------------------------
		self.foregroundRects = foregroundRects

	# --------------
	def Enable(self):
	# --------------
		self.enabled = True
		
	# ---------------
	def Disable(self):
	# ---------------
		self.enabled = False

	# ------------------------------
	def onLeftMouseDown(self, event):
	# ------------------------------
		if self.enabled:
			self.drawing = True
			self.firstPt = event.GetPosition()

	# ----------------------------
	def onLeftMouseUp(self, event):
	# ----------------------------
		if self.enabled:
			pos = event.GetPosition()
			rect = self.getRect(pos, self.firstPt)
			self.drawing = False

			if self.callback != None:
				self.callback(rect)

	# --------------------------
	def onMouseMove(self, event):
	# --------------------------
		if self.enabled and self.drawing:
			pos = event.GetPosition()
			rect = self.getRect(pos, self.firstPt)
		
			self.draw([rect], self.backgroundRects)
		
	# -------------------------------------------------
	def drawEvt(self, foregroundRects, backgroundRects):
	# -------------------------------------------------
		self.foregroundRects = foregroundRects
		self.backgroundRects = backgroundRects

	# ----------------------
	def onPaint(self, event):
	# ----------------------
		self.draw(self.foregroundRects, self.backgroundRects)

	# ----------------------------------------------
	def draw(self, foregroundRects, backgroundRects):
	# ----------------------------------------------
		# Setup the drawing context		
		dc = wx.PaintDC(self)
		dc.SetBackgroundMode(wx.TRANSPARENT)
		dc.BeginDrawing()
		
		# Background 
		Y_OFF = 0
		dc.DrawBitmap(self.image, 0, Y_OFF ) # The offset is ugly and this is a terrible way to do this...
		
		# Background rects
		backgroundPen = wx.Pen(self.backgroundColor, self.penWidthPx)
		dc.SetPen(backgroundPen)
		dc.SetBrush(wx.Brush((256, 0,0), wx.TRANSPARENT))		
		for rect in backgroundRects:
			if rect[0] > 0 and rect[1] > 0 and rect[2] > 0 and rect[3] > 0:
				dc.DrawRectangle(rect[0], rect[1] + Y_OFF, rect[2], rect[3])
		
		# Foreground rects
		foregroundPen = wx.Pen(self.foregroundColor, self.penWidthPx)
		dc.SetPen(foregroundPen)
		dc.SetBrush(wx.Brush((0, 256,0), wx.TRANSPARENT))		
		for rect in foregroundRects:
			if rect[0] > 0 and rect[1] > 0 and rect[2] > 0 and rect[3] > 0:
				dc.DrawRectangle(rect[0], rect[1] + Y_OFF, rect[2], rect[3])

		dc.EndDrawing()

			
	# -------------------------	
	def getRect(self, pt1, pt2):
	# -------------------------	
		""" Convert from top left + bottom right to top left + width & height """ 
		x1 = min(pt1[0], pt2[0])
		y1 = min(pt1[1], pt2[1]) 
		x2 = max(pt1[0], pt2[0])
		y2 = max(pt1[1], pt2[1])
	
		w = x2 - x1 
		h = y2 - y1

		return(x1, y1, w, h)
